<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Search extends CI_Controller {
	function __construct(){
            parent:: __construct();
            $this->load->model('transaksi/mpenjualan','',TRUE);
            $this->load->model('master/mpelanggan','',TRUE);
            $this->load->model('master/msales','',TRUE);
            $this->load->model('master/msupplier','',TRUE);
    }
	
	function index()
	{
		$this->barang();
	}

	public function barang(){
		$term    = $_GET['term'];
		$barang  = $this->mpenjualan->getbarang($term)->result();
		$json    = '[';	
		foreach($barang as $row){
			$nama = str_replace( '"', "", $row->nama );
			
			if(strlen($json) > 2)
				$json .= ',';
				$json .= '{"value":"' 
				.$row->prokode. 	"-" 
				.$nama . 			"-" 
				.$row->harga1. 		"-" 
				.$row->satuan.		'"
				,"label":"' . $row->prokode . " - " . $nama. '"}'; 	
		}	
		$json .= ']';	
		echo $json;
	}

	function pelanggan(){
		$term    = $_GET['term'];
		$barang  = $this->mpelanggan->getcus($term)->result();
		$json    = '[';
		foreach($barang as $row){
			$nama = str_replace( '"', "", $row->nama );
			
			if(strlen($json) > 2)
				$json .= ',';
			$json .= '{"value":"'.$row->pelanggan_id."-".$nama."-".$row->alamat."-".$row->kota."-".$row->tlp.'","label":"' . $row->pelanggan_id . " - " . $nama. '"}'; 	
		}	
		$json .= ']';	
		echo $json;
	}

	function sales(){
		$term    = $_GET['term'];
		$barang  = $this->msales->getsales($term)->result();
		$json    = '[';
		foreach($barang as $row){
			$nama = str_replace( '"', "", $row->nama );
			
			if(strlen($json) > 2)
				$json .= ',';
			$json .= '{"value":"'.$row->sales_id."-".$nama."-".$row->alamat."-".$row->kota."-".$row->tlp.'","label":"' . $row->sales_id . " - " . $nama. '"}'; 	
		}	
		$json .= ']';	
		echo $json;
	}
	
	function supplier(){
		$term    = $_GET['term'];
		$supp  	 = $this->mpenjualan->getsupp($term)->result();		
		$json    = '[';
		foreach($supp as $row){
			$nama = str_replace( '"', "", $row->nama );
			
			if(strlen($json) > 2)
				$json .= ',';
			$json .= '{"value":"'.$row->kode. "-"
								 .$nama. "-"
								 .$row->alamat. "-"
								 .$row->kota. "-"
								 .$row->kodepos."-"
								 .$row->tlp. "-"
								 .$row->fax. "-"
								 .$row->email. "-"
								 .$row->web. '"
								 ,"label":"' .$row->kode. " - " .$nama.'"}'; 	
		}	
		$json .= ']';	
		echo $json;
	}	
}
